package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.Constant;
import com.mall.exception.MallException;
import com.mall.exception.MallExceptionEnum;
import com.mall.mapper.CategoryMapper;
import com.mall.mapper.ProductMapper;
import com.mall.model.query.ProductListQuery;
import com.mall.model.dto.ProductListRequest;
import com.mall.model.po.Product;
import com.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addProduct(Product product) {
        Product oldProduct =productMapper.selectProductByName(product.getName());
        if (oldProduct!=null){
            throw new MallException(MallExceptionEnum.NAME_EXISTS);
        }
        int count =productMapper.insertSelective(product);
        if (count==0) {
            throw new MallException(MallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public void updateProduct(Product product) {
       if(product.getName()!=null){
           Product oldProduct=productMapper.selectProductByName(product.getName());
           if (oldProduct != null && product.getId() != oldProduct.getId() && product.getName().equals(oldProduct.getName())) {
                throw new MallException(MallExceptionEnum.NAME_EXISTS);
           }
       }
       int count =productMapper.updateByPrimaryKeySelective(product);
        if (count==0) {
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void deleteProduct(Integer id) {
        Product product =productMapper.selectByPrimaryKey(id);
        if(product==null){
            throw  new MallException(MallExceptionEnum.NOT_EXIST_PRODUCT);
        }
        int count =productMapper.deleteByPrimaryKey(id);
        if(count==0){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void updateBatchSellStatus(Integer[] ids, Integer sellStatus) {
        productMapper.updateBatchSellStatus(ids,sellStatus);
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,"update_time desc");
        List<Product> products = productMapper.selectByExample(null);
        PageInfo pageInfo= new PageInfo(products);
        return pageInfo;
    }
    @Override
    public Product detail(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public PageInfo listForCustomer(ProductListRequest productListRequest) {
        //创建查询搜索条件
        ProductListQuery productQuery = new ProductListQuery();
        //关键字判断
        if(StringUtils.hasText(productListRequest.getKeyword())){
            String keyword = new StringBuffer().append("%")
                    .append(productListRequest.getKeyword())
                    .append("%").toString();
            productQuery.setKeyword(keyword);
        }
        //分类id判断
        if(productListRequest.getCategoryId()!=null){
//           当用户选择了分类查询时
            List<Integer> categoryIds = getCategoryIdsByParentId(productListRequest.getCategoryId());
            productQuery.setCategoryIds(categoryIds);
        }
        //排序条件
        if(productListRequest.getOrderBy()!=null
                && Constant.ProductListOrderBy.set.contains(productListRequest.getOrderBy())){
            //开启分页
            PageHelper.startPage(productListRequest.getPageNum(), productListRequest.getPageSize(),productListRequest.getOrderBy());
        } else {
            PageHelper.startPage(productListRequest.getPageNum(), productListRequest.getPageSize());
        }
        // 根据条件查询
        List<Product> list = productMapper.selectListByProductQuery(productQuery);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
//    根据分类查询当前包含的子id
    private List<Integer> getCategoryIdsByParentId(Integer categoryId) {
//    创建所有id的列表
        List<Integer> ids =categoryMapper.selectIdByParentId(categoryId);
        ids.add(categoryId);
        return ids;

}
}
